﻿using System;
using System.Threading;
using System.Windows.Forms;
using QQNumberForZoneCatchService;
using Spider.Service;
using Timer = System.Windows.Forms.Timer;

namespace QQNumberForZoneCatchSys
{
    public partial class Main : Form
    {
        private static int ReviveTimes { get; set; }//复活次数
        private static int ReviveIntervalTime { get; set; }//复活间隔时间，小时
        private Timer timer1;
        private Timer timer11;
        public Main()
        {
            InitializeComponent();
            timer1 = new Timer();
            timer1.Interval = 500;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
            timer11 = new Timer();
            timer11.Interval = 2000;
            timer11.Tick += new EventHandler(timer11_Tick);
        }

        private void Main_Load(object sender, EventArgs e)
        {
            ReviveTimes = (int)nudSystemReveTimes.Value;
            ReviveIntervalTime = (int)nudSystemReveInterval.Value;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            lblSpriderRemainTask.Text = SpiderMonitor.GetTodoTaskCount().ToString();
            lblAntRemainTask.Text = SpiderMonitor.RemainAntTask.ToString();
            lblAntFinishedCount.Text = SpiderMonitor.GetVisitedCount().ToString();
            lblSpriderWorking.Text = SpiderMonitor.SpiderIsWorking ? "是" : "否";
            lblAntWorking.Text = SpiderMonitor.AntIsWorking ? "是" : "否";
            btnSpriderStartToWork.Text = SpiderMonitor.SpiderIsWorking ? "停止工作" : "开始工作";
            btnAntStartToWork.Text = SpiderMonitor.AntIsWorking ? "停止工作" : "开始工作";
            lblSpriderCaptainreason.Text = SpiderMonitor.SpiderCaptainStopWorkReason;
            lblAntCaptainReason.Text = SpiderMonitor.AntCaptainStopWorkReason;
            lblAntRemainCount.Text = SpiderMonitor.RemainAntTask.ToString();
            lblAntSuccessFinishedCount.Text = SpiderMonitor.AntSuccessFinishedTaskCount.ToString();
            lblAntFinishedCount.Text = SpiderMonitor.AntFinishedTaskCount.ToString();
            lblSpiderCaptainStartTime.Text = SpiderMonitor.SpiderCaptainStartWorkTime.ToString();
            lblSpiderCaptainEndTime.Text = SpiderMonitor.SpiderCaptainEndWorkTime.ToString();
            lblQQCurrentSeed.Text = QQNumberForZoneService.CurrentQqSeed;
            lblRemainReviewTimes.Text = ReviveTimes.ToString();
            if (SpiderMonitor.AntFinishedTaskCount != 0)
            {
                lblAntSuccesRate.Text =
              (Math.Round(((double)SpiderMonitor.AntSuccessFinishedTaskCount / (double)SpiderMonitor.AntFinishedTaskCount), 2)*100)+"%";
            }
          
        }
        private void timer11_Tick(object sender, EventArgs e)
        {
            QQNumberForZoneService.AutoSendQQSeed((int)nudQQSeedCount.Value);
            //成功率如果小于X%则停止爬虫，需要检测。
            var successrate = Convert.ToDouble(lblAntSuccesRate.Text.Replace("%",""));
            if (successrate!=0&&successrate < (double) (nudMinSuccessRate.Value))
            {
                SpiderService.CommandSpiderStopToWork("成功率小于最低值，请检查系统");
                AntService.CommandAntCaptainStopToWork("成功率小于最低值，请检查系统");
            }
            //系统自动重启
            if (ReviveTimes > 0 && SpiderMonitor.SpiderCaptainEndWorkTime != DateTime.MinValue && DateTime.Now - SpiderMonitor.SpiderCaptainEndWorkTime > new TimeSpan(0, 0, ReviveIntervalTime, 0))
            {
                btnQQStart_Click(null, null);
                ReviveTimes--;
            }
        }
        private void label6_Click(object sender, EventArgs e)
        {

        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            ThreadPool.SetMaxThreads((int)nudThreadpool.Value, (int)nudThreadpool.Value);
        }

        private void nupSpriderWorkSpeed_ValueChanged(object sender, EventArgs e)
        {
            SpiderService.SetSpriderCaptainWorkSpeed((int)nupSpriderWorkSpeed.Value*1000);
        }

        private void btnSpriderStartToWork_Click(object sender, EventArgs e)
        {
            if (btnSpriderStartToWork.Text == "开始工作")
                SpiderService.CommandSpiderToWork();
            else
            {
                SpiderService.CommandSpiderStopToWork();
            }

        }

        private void btnAntStartToWork_Click(object sender, EventArgs e)
        {
            if (btnAntStartToWork.Text == "开始工作")
                AntService.CommandAntCaptainToWork();
            else
            {
                AntService.CommandAntCaptainStopToWork();
            }
        }

        private void btnQQStart_Click(object sender, EventArgs e)
        {

            QQNumberForZoneService.StartToQQEmailGenerate();
            timer11.Start();
            MessageBox.Show("已经开始");
        
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void nudSystemReveTimes_ValueChanged(object sender, EventArgs e)
        {
            ReviveTimes = (int)nudSystemReveTimes.Value;
        }

        private void nudSystemReveInterval_ValueChanged(object sender, EventArgs e)
        {
            ReviveIntervalTime = (int)nudSystemReveInterval.Value;
        }

    }
}
